package quanlydoanra.utils;

import java.sql.ResultSet;
import java.sql.SQLException;

import quanlydoanra.themmoi.MucDich;
import quanlydoanra.themmoi.NguoiDi;
import quanlydoanra.theotieuchithoigian.ThongTin;
import utils.DateUtils;
import utils.UtilsOnScopes;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import dbutil.ConnectDB;

public class truyXuatDatabase {
	
	/**************************************
	 * Method name		: insertData
	 * Return type		: int
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static int insertData(String SQL) {
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		int numberRowsUpdated = -1;
				try {
				numberRowsUpdated = cont.getStatement().executeUpdate(SQL);
			} catch (SQLException se) {
				System.out.println(" SQL is invalid");
				se.printStackTrace();
			}
		return numberRowsUpdated;
	}
	/**************************************
	 * Method name		: kiemTraSoHC
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static boolean kiemTraSoHC(String soHC){
		boolean result = false;
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		String sql = "select * from hochieu where LOWER(TRIM(sohc)) LIKE LOWER(TRIM('"
			+ soHC + "'))";
		ResultSet resultSet = null;
		int i =0;
		try {
			resultSet = cont.getStatement().executeQuery(sql);
			while(resultSet.next()){
				i++;
			}
			if (i > 0){//Tồn tại trong CSDL
				result = true;
			}
		} catch (SQLException se) {
			System.out.println(" SQL is invalid");
			se.printStackTrace();
		}
		return result;
	}
	//Lay ra id cua Thanh Vien cu the trong doan di do
	/**************************************
	 * Method name		: getIDThanhVien
	 * Return type		: int
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static int getIDThanhVien(String madoanra, String soHC){
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		int acc =-1;
		ResultSet resultSet = null;
		try{
			String sql = "select id from dsnguoidi where iddoanra = '"+ madoanra.trim() + "' and sohc = '"+ soHC.trim() + "'";
			resultSet = cont.getStatement().executeQuery(sql);
			resultSet.next();
			acc = resultSet.getInt("id");
			}
		catch(Exception ex){
			System.out.println(ex.toString());
		}
		return acc;
	}
	//Lay ra id cua Thanh Vien cu the trong doan vao do
	/**************************************
	 * Method name		: getIDThanhVienDoanVao
	 * Return type		: int
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static int getIDThanhVienDoanVao(String madoanvao, String soHC, String cmnd){
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		int acc =-1;
		ResultSet resultSet = null;
		try{
			String sql = "select id from dsnguoidi where iddoanvao = '"+madoanvao+"' and sohc = '"+soHC+"' and cmnd = '"+cmnd+"'";
			resultSet = cont.getStatement().executeQuery(sql);
			resultSet.next();
			acc = resultSet.getInt("id");
			}
		catch(Exception ex){
			System.out.println(ex.toString());
		}
		return acc;
	}
	
	/**************************************
	 * Method name		: searchHelp
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String searchHelp(String userName, String groupId) {
		StringBuffer buffer = new StringBuffer();
		String strTemp = "";
		String strTempCond1 = "uname LIKE '%" + userName + "%' OR '"
				+ (userName.equals("-1") ? "0" : userName) + "'='0'";
		String strTempCond2 = "gid LIKE '" + groupId + "' OR '"
				+ (groupId.equals("-1") ? "0" : groupId) + "'= '0'";

		strTemp = "(" + strTempCond1 + ") AND (" + strTempCond2 + ")";
		buffer.append("(");
		buffer.append(strTemp);
		buffer.append(")");
		return buffer.toString();
	}
	/**************************************
	 * Method name		: layThongTinCaNhan
	 * Return type		: String[]
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static String[] layThongTinCaNhan(String soHC){
		String[] str = new String[6];
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		int acc =-1;
		ResultSet resultSet = null;
		try{
			
			String sql0 = "select cmnd from hochieu where sohc = '"+soHC+"'";			
			ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(sql0);
			if(array.size() >0){
			HashMap<String, Object> hash = new HashMap<String, Object>();
			hash = (HashMap<String, Object>) array.get(0);
			String cmnd = hash.get("cmnd").toString();
			String sql = "select * from canhan where cmnd = '"+cmnd+"'";			
			ArrayList<HashMap<String, Object>> arrayCaNhan = ConnectDB.getDataTypeList(sql);
			
			if(arrayCaNhan.size() >0){
			HashMap<String, Object> hashCaNhan = new HashMap<String, Object>();
			hashCaNhan = (HashMap<String, Object>) arrayCaNhan.get(0);
		
			String ten = hashCaNhan.get("ten").toString();
			String namSinh = hashCaNhan.get("namsinh").toString();
			boolean gioiTinh = Boolean.parseBoolean(hashCaNhan.get("gioitinh").toString());
			System.out.println("Gioi Tinh "+gioiTinh);
			String strGioiTinh ="";
			if(gioiTinh == true)strGioiTinh ="Nam";
			else strGioiTinh ="Nữ";
			String coQuan =  hashCaNhan.get("coquan").toString();
			String chucVu  = hashCaNhan.get("chucvu").toString();
			str[0] = cmnd;
			str[1] = ten;
			str[2] = namSinh;
			str[3] = strGioiTinh;
			str[4] = coQuan;
			str[5] = chucVu;			
			}
			else {
				str[0] = "";
				str[1] = "";
				str[2] = "";
				str[3] = "";
				str[4] = "";
				str[5] = "";
			}
			}
			else {
				str[0] = "";
				str[1] = "";
				str[2] = "";
				str[3] = "";
				str[4] = "";
				str[5] = "";
			}
		}
		catch(Exception ex){
			System.out.println(ex.toString());
		}
		return str;
	}
	/**************************************
	 * Method name		: kiemTraThanhVien
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static boolean kiemTraThanhVien(String cmnd, String maDoan){
		boolean result = false;
		String sql = "select * from dsnguoidi where cmnd = '"+cmnd+"' and iddoanra = '"+maDoan+"'";
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		int i =0;
		ResultSet resultSet = null;
		try{
			resultSet = cont.getStatement().executeQuery(sql);
			while(resultSet.next()){
				i++;
			}
			if (i > 0){//Tồn tại trong CSDL
				result = true;
			}
		}
		catch(Exception ex){
			}
		return result;
	}
	/**************************************
	 * Method name		: getDateInDoanRa
	 * Return type		: Date[]
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: NGoc Diem
	 **************************************/
	public static Date[] getDateInDoanRa(String maDoan){
		Date[] d = new Date[4];
		try{
		String sql = "select * from doanra where madoan = '"+ maDoan.trim() + "'";
		ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(sql);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
				hash = (HashMap<String, Object>) array.get(0);
				Date ngayQuyDinhNop = null;
				Date ngayDi  = java.sql.Date.valueOf(hash.get("ngaydi").toString());
				Date ngayVe = java.sql.Date.valueOf(hash.get("ngayve").toString());
				int IDQD  = Integer.parseInt(hash.get("idquyetdinh").toString());
				String sql1 = "select * from quyetdinh where id = "+IDQD;
				
				ArrayList<HashMap<String, Object>> arrayQD = ConnectDB.getDataTypeList(sql1);
				if (arrayQD.size() > 0) {
					HashMap<String, Object> hashQD = new HashMap<String, Object>();
						hashQD = (HashMap<String, Object>) arrayQD.get(0);		
						Date ngayKyQD = java.sql.Date.valueOf(hashQD.get("ngaykyqd").toString());
						d[0] = ngayKyQD;
						d[1] = ngayDi;
						d[2] = ngayQuyDinhNop;
						d[3] = ngayVe;
				}
				else {
					d[0] = null;
					d[1] = null;
					d[2] = null;
				}
			}
		else {
			d[0] = null;
			d[1] = null;
			d[2] = null;
		}		
		}
		catch(Exception ex){
		}
		return d;
	}
	/**************************************
	 * Method name		: updateNguoiDi
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static boolean updateNguoiDi(String maDoan, String tenNguoiDi,String cmnd, String soHC, boolean trangThaiDi){
		boolean result = false;
		String sql ="update dsnguoidi set tennguoidi = '"+tenNguoiDi+"'," +
										"cmnd = '"+cmnd+"'," +
										"sohc = '"+soHC+"'," +
										"trangthaidi = "+trangThaiDi+"," +
										" where cmnd = '"+cmnd+"' and iddoanra = '"+maDoan +"'";
		System.out.println("update "+sql);
			
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		try{
		int i = cont.getStatement().executeUpdate(sql);
		if (i >0) result = true;
		}catch(Exception ex){
			result = false;
		}
			return result;
	}
	/**************************************
	 * Method name		: deleteNguoiDi
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: NGoc Diem
	 **************************************/
	public static boolean deleteNguoiDi(int id){
		boolean result = false;		
		String sql = "delete from dsnguoidi where id = "+id+"";
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		try{
			int i = cont.getStatement().executeUpdate(sql);
			if (i >0) result = true;
			}catch(Exception ex){
				result = false;
			}
				return result;
		}
	/**************************************
	 * Method name		: xoaDoanRa
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static boolean xoaDoanRa(String maDoan){
		boolean result = false;		
		String sql =" update doanra set flagdelete = 1  where madoan = '"+ maDoan.trim() + "'";
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		try{
			int i = cont.getStatement().executeUpdate(sql);
			if (i >0) result = true;
			}catch(Exception ex){
				result = false;
			}
				return result;
		}
/**************************************
 * Method name		: kiemTraThanhVienDaVeChua
 * Return type		: boolean
 * Description		:
 * Created date		: Aug 12, 2008
 * Author			: Ngoc Diem
 **************************************/
public static boolean kiemTraThanhVienDaVeChua(String cmnd ,java.sql.Date ngayDiDot2,java.sql.Date ngayVeDot2){
	boolean result = false;
	ConnectDB cont = new ConnectDB();
	cont.connectDB();
	//ngay ve dot 1 truoc (nho hon) ngay di dot hai la dc phep di
	String sql = "select * from dsnguoidi as DS, doanra as DR where DS.iddoanra = DR.madoan and DS.cmnd = '"+cmnd+"' and ( DR.ngayve > '"+ngayDiDot2+"' or DR.ngaydi < '"+ngayVeDot2+"')";
	//neu co dong du lieu la ko dc di
	System.out.println("SQL truy van "+sql);
	ResultSet resultSet = null;
	int i =0;
	try {
		resultSet = cont.getStatement().executeQuery(sql);
		while(resultSet.next()){
			i++;
		}
		if (i > 0){//Tồn tại trong CSDL
			result = true;
		}
	} catch (SQLException se) {
		System.out.println(" SQL is invalid");
		se.printStackTrace();
	}
	return result;
}
/**************************************
 * Method name		: capNhatDoanRa
 * Return type		: boolean
 * Description		:
 * Created date		: Aug 12, 2008
 * Author			: Ngoc Diem
 **************************************/
public static boolean capNhatDoanRa(String maDoan, String loaiDoan,boolean baocaoKq, String nuocDen, String linhVuc, String mucDich, java.sql.Date ngayDi, java.sql.Date ngayVe, String soQD, java.sql.Date ngayKyQD, String nguoiKyQD, String kinhPhi, String donViMoi){
	String sql = "select * from doanra where madoan = '"+ maDoan.trim() + "' and flagdelete = 0";
	String sqlUpdateDoanRa = "update doanra set loaidoan = '"+loaiDoan+"'," +
	"nuocden = '"+nuocDen+"'," +
	" ngaydi = '"+ngayDi+"'," +
	" ngayve = '"+ngayVe+"', "+
	"donvimoi = '"+donViMoi+"', "+
	"chiphi = '"+kinhPhi+"', "+
	"baocaokq = "+baocaoKq +" " +
		" where madoan = '"+maDoan+"'";

	boolean result = false;
	ConnectDB cont = new ConnectDB();
	cont.connectDB();
	ResultSet resultSet = null;
	System.out.println("SQL update doan ra "+sqlUpdateDoanRa);
	int i =0;
	try {
		resultSet = cont.getStatement().executeQuery(sql);
		if(resultSet.next()){
			int idQD = resultSet.getInt("idquyetdinh");
			int idMucDich = resultSet.getInt("idmucdich");
			String sqlUpdateQD ="update quyetdinh set soqd = '"+soQD+"', " +
												"nguoikyqd = '"+nguoiKyQD+"', " +
												"ngaykyqd = '"+ngayKyQD+"' where id = "+idQD;
			
			String sqlUpdateMucDich = "update mucdich set linhvuc = '"+linhVuc+"', " +
															"mucdich = '"+mucDich+"' where id = "+idMucDich;
			int updateDoanRa = cont.getStatement().executeUpdate(sqlUpdateDoanRa);
			int updateQD = cont.getStatement().executeUpdate(sqlUpdateQD);
			int updateMucDich = cont.getStatement().executeUpdate(sqlUpdateMucDich);
			System.out.println("SQL update quyet dinh "+idQD+" "+sqlUpdateQD);
			System.out.println("SQL update muc dihc "+idMucDich+" "+sqlUpdateMucDich);
			if(updateDoanRa > 0 && updateQD > 0 && updateMucDich >0){
				result = true;
			}
			else result = false;
		}
		else result = false;
	} catch (SQLException se) {
		System.out.println(" SQL is invalid");
		se.printStackTrace();
	}
	return result;
}
	/**************************************
	 * Method name		: getDuyet
	 * Return type		: int
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static int getDuyet(String maDoan){
		String userName = UtilsOnScopes.getUserName();
		int result = 0;
		String SQL="SELECT * FROM phanquyenxoasua A" +
				"  WHERE A.khoa=0 " +
				" AND A.sohc_idgroup ='"+maDoan+"' " +
				" AND A.username='"+userName+"'";
		System.out.println("SQL DUYET "+SQL);
		ArrayList<HashMap<String, Object>> array = ConnectDB
		.getDataTypeList(SQL);
		try {
			if (array.size() > 0) {
				HashMap<String, Object> hash = new HashMap<String, Object>();
				hash = (HashMap<String, Object>) array.get(0);
				int duyet = Integer.parseInt(hash.get("daduyet").toString());
				result = duyet;
			}
			else result = -1;
		}
		catch(Exception ex){}
		return result;
		
	}
/**************************************
 * Method name		: kiemTraDoanDaDuyet
 * Return type		: boolean
 * Description		:
 * Created date		: Aug 12, 2008
 * Author			: Ngoc Diem
 **************************************/
public static boolean kiemTraDoanDaDuyet(String maDoan){
	String userName = UtilsOnScopes.getUserName();
	boolean result = false;
	String SQL="SELECT COUNT(*) as count FROM phanquyenxoasua A" +
			"  WHERE A.daduyet=1 AND A.khoa=0 " +
			" AND A.sohc_idgroup ='"+maDoan+"' " +
			" AND A.username='"+userName+"'";
	System.out.println(SQL);
	ArrayList<HashMap<String, Object>> array = ConnectDB
	.getDataTypeList(SQL);
	try {
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			hash = (HashMap<String, Object>) array.get(0);
			String countStr = hash.get("count").toString();
			int count = Integer.parseInt(countStr);
			if(count > 0){
				return result = true;
			}
			else 
				return result = false;
		}else{
			result = false;
		}
	} catch (Exception e) {
		result = false;
		e.printStackTrace();
	}
	System.out.println("KET QUA123 "+result);
	return result;
}

/**************************************
 * Method name		: deleteNguoiVao
 * Return type		: boolean
 * Description		:
 * Created date		: Aug 12, 2008
 * Author			: Ngoc Diem
 **************************************/
public static boolean deleteNguoiVao(int id){
	boolean result = false;		
	String sql = "delete from dsnguoiden where id = "+id+"";
	System.out.println("SQL delete "+sql);
	ConnectDB cont = new ConnectDB();
	cont.connectDB();
	try{
		int i = cont.getStatement().executeUpdate(sql);
		if (i >0) result = true;
		}catch(Exception ex){
			result = false;
		}
			return result;
	}

/**************************************
 * Method name		: updateNguoiVao
 * Return type		: boolean
 * Description		:
 * Created date		: Aug 12, 2008
 * Author			: Ngoc Diem
 **************************************/
public static boolean updateNguoiVao(String maDoan,int id, String tenNguoiDi,String ngheNghiep,String cmnd, String soHC, Date ngayCapHC, Date ngayHetHanHC, Date ngayHetHanVisa){
	boolean result = false;
	String sql = "update dsnguoiden set ten = '"+tenNguoiDi+"'," +
									"cmnd = '"+cmnd+"'," +
									"sohc = '"+soHC+"'," +
									"nghenghiep = '"+ngheNghiep+"'," +
									"ngaycaphc = '"+ngayCapHC+"'," +
									"ngayhethanhc = '"+ngayHetHanHC+"'," +
									"ngayhethanvisa = '"+ngayHetHanVisa+"'" +
									" where id = "+id+" and iddoanvao = '"+ maDoan.trim() + "'";  
	
	System.out.println("SQL UPDATE "+sql);
	ConnectDB cont = new ConnectDB();
	cont.connectDB();
	try{
	int i = cont.getStatement().executeUpdate(sql);
	if (i >0) result = true;
	}catch(Exception ex){
		result = false;
	}
		return result;
}

/**************************************
 * Method name		: kiemTraThanhVienVao
 * Return type		: boolean
 * Description		:
 * Created date		: Aug 12, 2008
 * Author			: NGoc Diem
 **************************************/
public static boolean kiemTraThanhVienVao(String cmnd, String maDoan){
	boolean result = false;
	String sql = "select * from dsnguoiden where cmnd = '"+cmnd+"' and iddoanvao = '"+ maDoan.trim() + "'";  
	ConnectDB cont = new ConnectDB();
	cont.connectDB();
	int i =0;
	ResultSet resultSet = null;
	try{
		resultSet = cont.getStatement().executeQuery(sql);
		while(resultSet.next()){
			i++;
		}
		if (i > 0){//Tồn tại trong CSDL
			result = true;
		}
	}
	catch(Exception ex){
		}
	return result;
}

/**************************************
 * Method name		: getDate_Den_DotMoi_InDoanVao
 * Return type		: Date
 * Description		:
 * Created date		: Aug 12, 2008
 * Author			: Ngoc Diem
 **************************************/
public static Date getDate_Den_DotMoi_InDoanVao(String maDoan){
	String sql = "select * from doanvao where madoan = '"+maDoan+"'";
	ConnectDB cont = new ConnectDB();
	Date d = null;
	cont.connectDB();
	int acc =-1;
	ResultSet resultSet = null;
	try{
	resultSet = cont.getStatement().executeQuery(sql);
	if(resultSet.next()){
	d  = resultSet.getDate("ngayden");
	}
	}
	catch(Exception ex){
	}
	return d;
}

/**************************************
 * Method name		: kiemTraThanhVienCoDangTrongNuocKhong
 * Return type		: boolean
 * Description		:
 * Created date		: Aug 12, 2008
 * Author			: Ngoc Diem
 **************************************/
public static boolean kiemTraThanhVienCoDangTrongNuocKhong(String cmnd ,java.sql.Date ngayHetHanVisa,java.sql.Date ngayDenDotMoi){
	boolean result = false;
	ConnectDB cont = new ConnectDB();
	cont.connectDB();
	//ngay ve dot 1 truoc (nho hon) ngay di dot hai la dc phep di
	String sql = "select * from dsnguoiden as DS, doanvao as DR where DS.iddoanvao = DR.madoan and DS.cmnd = '"+cmnd+"' and ( DR.ngayden < '"+ngayHetHanVisa+"')";
	//neu co dong du lieu la ko dc di
	System.out.println("SQL truy van "+sql);
	ResultSet resultSet = null;
	int i =0;
	try {
		resultSet = cont.getStatement().executeQuery(sql);
		while(resultSet.next()){
			i++;
		}
		if (i > 0){//Tồn tại trong CSDL
			result = true;
		}
	} catch (SQLException se) {
		System.out.println(" SQL is invalid");
		se.printStackTrace();
	}
	return result;
}
	/**************************************
	 * Method name		: capNhatDoanVao
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: NGoc Diem
	 **************************************/
	public static boolean capNhatDoanVao(String maDoan,
			java.sql.Date ngayDen, java.sql.Date ngayVe,
			String mucDich,String linhVuc, String diaDiemDen, String soVB,
			String cqDeNghi, java.sql.Date ngayXuatVB,
			String noiDungVBDen, String moTaQDDen, String soUBNDTinh,
			java.sql.Date ngayPhatHanh, boolean baoCao,
			String nguoiChuTri, String moTaQDNhan, String moTa) {
		// TODO Auto-generated method stub
		MucDich mD = new MucDich(linhVuc,mucDich);
		int idmucDich = mD.timLinhVuc();		
		String sql = "select * from doanvao where madoan = '"+maDoan +"' and flagdelete = 0 ";
		String sqlUpdateDoanVao = "update doanvao set ngayden = '"+ngayDen+"'," +
								"ngaydi = '"+ngayVe+"'," +
								" idmucdich = '"+idmucDich+"', "+
								" diadiemlamviec = '"+diaDiemDen+"'," +
								" mota = '"+moTa+"' "+
								" where madoan = '"+maDoan+"'";

		boolean result = false;
		ConnectDB cont = new ConnectDB();
		cont.connectDB();		
		ResultSet resultSet = null;
		System.out.println("SQL update doan vao"+sqlUpdateDoanVao);
		int i =0;
		try {
			resultSet = cont.getStatement().executeQuery(sql);
			if(resultSet.next()){
				int idVBD = resultSet.getInt("idvbden");
				int idVBN = resultSet.getInt("idvbvao");
				int idMucDich = resultSet.getInt("idmucdich");
				
				String sqlUpdateVBD ="update vanbanden set sovb = '"+soVB+"', " +
													"coquandenghi = '"+cqDeNghi+"', " +
													"noidung = '"+noiDungVBDen+"', " +
													"motaqd = '"+moTaQDDen+"', " +
													"ngayxuatvb = '"+ngayXuatVB+"' where id = "+idVBD;
				
				String sqlUpdateVBN ="update vanbannhan set soubndtinh = '"+soUBNDTinh+"', " +
									"ngayphathanh = '"+ngayPhatHanh+"', " +
									"baocao = '"+baoCao+"', " +
									"nguoichutri = '"+nguoiChuTri+"', " +
									"motaqd = '"+moTaQDNhan+"' where id = "+idVBN;

				System.out.println("SQL update quyet dinh "+idVBD+" "+sqlUpdateVBD);
				System.out.println("SQL update quyet dinh "+idVBN+" "+sqlUpdateVBN);
			
				int updateDoanVao = cont.getStatement().executeUpdate(sqlUpdateDoanVao);
				int updateVBD = cont.getStatement().executeUpdate(sqlUpdateVBD);
				int updateVBN = cont.getStatement().executeUpdate(sqlUpdateVBN);
		
				if(updateDoanVao > 0 && updateVBD > 0 && updateVBN > 0){
					result = true;
				}
				else result = false;
			}
			else result = false;
		} catch (SQLException se) {
			System.out.println(" SQL is invalid");
			se.printStackTrace();
		}
		return result;
	
	}
	
	/**************************************
	 * Method name		: xoaDoanVao
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static boolean xoaDoanVao(String maDoan){
		boolean result = false;		
		String sql =" update doanvao set flagdelete = 1  where madoan = '"+ maDoan.trim() + "'"; 
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		try{
			int i = cont.getStatement().executeUpdate(sql);
			if (i >0) result = true;
			}catch(Exception ex){
				result = false;
			}
				return result;
		}
	/**************************************
	 * Method name		: insertNhatKyXoaSua
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public static boolean insertNhatKyXoaSua(String username, java.sql.Date thoiGianXoa,String sohc_idgroup,String thaotacxoa_sua, String ghiChu){
		String sql = "insert into nhatkyxoasua(thanhvien,thoigianxoa_sua,sohc_idgroup,thaotacxoa_sua,ghichu) values('"+username+"','"+thoiGianXoa+"','"+sohc_idgroup+"','"+thaotacxoa_sua+"','"+ghiChu+"')";
		boolean result = false;	
		System.out.println("SQL Nhat ky "+sql);
		ConnectDB cont = new ConnectDB();
		cont.connectDB();
		try{
			int i = cont.getStatement().executeUpdate(sql);
			if (i >0) result = true;
			}catch(Exception ex){
				result = false;
			}
				return result;
		}
	
	static final long ONE_HOUR = 60 * 60 * 1000L;
	public long daysBetween(Date d1, Date d2)
	{
	return ( (d2.getTime() - d1.getTime() +
	ONE_HOUR) / (ONE_HOUR * 24));
	}
	
	}

